package com.zbkj.crmeb.chant.controller;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.github.pagehelper.PageInfo;
import com.utils.ExcelUtil;
import com.zbkj.crmeb.chant.model.ChannelProduct;
import com.zbkj.crmeb.chant.request.ChannelProductRequest;
import com.zbkj.crmeb.chant.request.ProductPushRequest;
import com.zbkj.crmeb.chant.service.IChannelProductService;
import com.zbkj.crmeb.chant.service.ITianMaoExcelService;
import com.zbkj.crmeb.chant.vo.ChannelProductVo;
import com.zbkj.crmeb.export.vo.ChannelProductExcelVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 渠道商品
 */
@Slf4j
@RestController("ChannelProductController")
@RequestMapping("api/chant/channel-product")
@Api(tags = "渠道商品")
public class ChannelProductController {

    @Autowired
    private IChannelProductService channelProductService;
    @Autowired
    private ITianMaoExcelService tianMaoExcelService;

    /**
     * 渠道商品列表
     * @return
     */
    @ApiOperation(value = "渠道商品列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ChannelProductVo>> productList(ChannelProductRequest reqParam, @Validated PageParamRequest pageParam){
        if(Objects.isNull(reqParam.getChannelId())){
            return CommonResult.failed("渠道id必传!");
        }
        PageInfo<ChannelProductVo> channelProductList = channelProductService.productList(reqParam, pageParam);
        return CommonResult.success(CommonPage.restPage(channelProductList));
    }
    
    /**
     * 导出
     * @Description:  
     * @author daigb 
     * @param @param reqParam
     * @param @param pageParam
     * @param @return
     * @return CommonResult<CommonPage<ChannelProductVo>>
     * @throws
     */
    @ApiOperation(value = "渠道商品导出")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void productExport(ChannelProductRequest reqParam, @RequestParam(defaultValue = "1") Integer page, HttpServletResponse response){
        List<ChannelProductExcelVo> list = tianMaoExcelService.excelTianMao(reqParam, page);
        log.info("--------------- 导出天猫商品, 当前数量: {}---------------", list.size());
        ExcelUtil.setSheetName("store");
        ExcelUtil.setFileName("渠道产品导出");
        ExcelUtil.writeExcel(response, list, ChannelProductExcelVo.class);
    }
    
    /**
     * 全部取消推送
     * @Description:  
     * @author daigb 
     * @param @param reqParam
     * @param @param response
     * @return void
     * @throws
     */
    @ApiOperation(value = "全部取消推送")
    @RequestMapping(value = "/cancel", method = RequestMethod.GET)
    public CommonResult<Boolean> productCancel(){
        log.info("--------------- 全部取消推送天猫商品 ---------------");
        channelProductService.update(Wrappers.<ChannelProduct>lambdaUpdate()
                .set(ChannelProduct :: getPushFlag, 2)
                );

        return CommonResult.success();
    }

    /**
     * 更新渠道商品
     */
    @ApiOperation(value = "更新渠道商品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<Boolean> updateProduct(@RequestBody ChannelProduct entity){
        if(Objects.isNull(entity.getChannelId())){
            return CommonResult.failed("渠道id必传!");
        }
        if(channelProductService.updateProduct(entity)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 推送渠道商品
     * @return
     */
    @ApiOperation(value = "推送渠道商品")
    @RequestMapping(value = "/push", method = RequestMethod.POST)
    public CommonResult<Boolean> pushProduct(@RequestBody ProductPushRequest pushRequest){
        if(Objects.isNull(pushRequest.getChannelId())){
            return CommonResult.failed("渠道id必传!");
        }
        if(channelProductService.pushProduct(pushRequest)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }
}
